﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebBookRecevie
{
    public partial class DynamicControl : System.Web.UI.Page
    {
        string[] pro1 = new string[] { "Name", "Age", "High", "From" };
        


        protected void Page_Load(object sender, EventArgs e)
        {
            bt_Save.Click += new EventHandler(bt_Save_Click);

            //

            DynamicAddControl();

        }

        void bt_Save_Click(object sender, EventArgs e)
        {
            person obj = new person() ;

            foreach (var item in DynamicArea.Controls)
            {
                if (item is TextBox)
                {
                    string fieldname = (item as TextBox).ID.Split('_')[1];
                    var field = obj.GetType().GetProperty(fieldname);
                    if (field != null)
                        field.SetValue(obj, (item as TextBox).Text, null);
                }
            }

            lb_result.Text += String.Format("{0}<br/>{1}<br/>{2}<br/>{3}<br/>", obj.Name, obj.Age, obj.High, obj.From);

        }

        private void DynamicAddControl()
        {

            foreach (var item in pro1)
            {
                TextBox tb = new TextBox();
                tb.ID = "Dy_" + item;
                tb.Text = "this's" + item;

                DynamicArea.Controls.Add(tb);
            }

        }

        class person
        {
            public string Name { get; set; }
            public string Age { get; set; }
            public string High { get; set; }
            public string From { get; set; }
        }


    }
}